package servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.util.Iterator;

import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dataBase.Code;
import dataBase.magaers.CodeDBMngr;

/**
 * Servlet implementation class SearchServlet
 */
public class SearchServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SearchServlet() {
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String category = request.getParameter("category");
		String inTitle = request.getParameter("title");
		String inCode = request.getParameter("withinCode");
		int page = Integer.valueOf(request.getParameter("page"));
		CodeDBMngr cdbm = CodeDBMngr.getInstance();
		if (cdbm == null){
			response.sendError(500);
			return;
		}
		Map<String, String> result = cdbm.search(inTitle, category, inCode, page);
		try {
			sendResult(result, response, page);
		} catch (IOException e) {
			response.sendError(500);
			return;
		}
	}
	private void sendResult(Map<String, String> result, HttpServletResponse response, int page) throws IOException {
		String answer;
		if ((result == null) || (result.size() == 0)){
			answer = "<html><head><title>search result</title></head>" +
				  "<body><h2>No mathes found for your query :(</h2><br />" +
				  "</body></html>";
		}
		else{
			answer = "<html><script type=\"text/javascript\" src=\"search.js\"></script><head><title>search result</title></head>" +
					  "<body><h2>The result for your query are the listed code sections:</h2><br />" +
					  "<ul>";
			Iterator<String> iter = result.keySet().iterator();
			while (iter.hasNext()) {
				String cid = iter.next();
				if ((!iter.hasNext()) && (result.size() > CodeDBMngr.ITEMS_IN_PAGE)){
					// last key
					break; // presenting until before last cid and title
				}
				String itemTitle = result.get(cid);
				answer += "<li id=\"" + cid + "\"><a href=\"javascript:showSearchResult('" + cid + "')\" >" + itemTitle + "</a></li>";
			}
			answer += "</ul><br />";
			
			// add next button
			answer += "<table><tr>";
			if (page > 1)
			{
				answer += "<td><a href=\"javascript:getPrev()\" >prev</a></td>"; 
			}
			    answer += "<td width=\"3\">&nbsp;</td>";
			if (result.size() > CodeDBMngr.ITEMS_IN_PAGE){
				answer += "<td><a href=\"javascript:getNext()\">next</a></td>";
			}
			
			answer += "</tr></table></body></html>";
		}
		response.setContentLength(answer.length());
		response.getOutputStream().print(answer);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.sendError(404);
		return;		
	}

}
